package com.example.mongodb.mongodb.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.internal.OSSBucketOperation;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.example.mongodb.mongodb.config.AliyunConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.FrameworkServlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * 实现aliyun-oss 文件上传下载
 */
@RestController
@RequestMapping("/oss")
public class OssAliyunController {


    @Autowired
    private OSSClient ossClient;

    @Autowired
    private AliyunConfig aliyunConfig;
    /**
     * 文件上传
     *
     * @param file
     */
    @GetMapping("/upload")
    public void upload(MultipartFile file) throws IOException {
        PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunConfig.getBucketName(), "oss_test", new ByteArrayInputStream(file.getBytes()));


        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        Callback callback = putObjectRequest.getCallback();

        System.out.println(putObjectResult);
        System.out.println(callback);


        // 关闭OSSClient。
        ossClient.shutdown();

    }


}
